﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;

namespace WebApplication1
{
    public partial class AddAthleticRecord : System.Web.UI.Page
    {
        StudentAL studentAL = new StudentAL();
        AthleticAL athleticAL = new AthleticAL();
        List<StudentDTO> studentDTO;
        List<AthleticEventDTO> athleticEventDTO;
        public Guid SelectedStudent
        {
            get
            {
                if (Session["StudentId"] == null)
                {
                    return studentAL.GetStudents()[0].StudentId;
                }
                else
                {
                    return new Guid(Session["StudentId"].ToString());
                }
            }
            set
            {
                Session["StudentId"] = value;
            }

        }
        public Guid SelectedAthleticEvent
        {
            get
            {
                if (Session["SelectedAthleticEventId"] == null)
                {
                    return athleticAL.GetAthleticEvents()[0].AthleticEventId;
                }
                else
                {
                    return new Guid(Session["SelectedAthleticEventId"].ToString());
                }
            }
            set
            {
                Session["SelectedAthleticEventId"] = value;
            }

        }
        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);

            athleticEventDTO =athleticAL.GetAthleticEvents();
            studentDTO = studentAL.GetStudents();
            

            ddlAthleticRecordAthleticEvent.DataSource = athleticEventDTO;
            ddlAthleticRecordAthleticEvent.DataTextField = "EventName";
            ddlAthleticRecordAthleticEvent.DataValueField = "AthleticEventId";
            ddlAthleticRecordAthleticEvent.SelectedValue = SelectedAthleticEvent.ToString();
            ddlAthleticRecordAthleticEvent.DataBind();

            ddlAthleticRecordStudentName.DataSource = studentDTO;
            ddlAthleticRecordStudentName.DataTextField = "FullName";
            ddlAthleticRecordStudentName.DataValueField = "StudentId";
            ddlAthleticRecordStudentName.SelectedValue = SelectedStudent.ToString();
            ddlAthleticRecordStudentName.DataBind();


            ddlPrevAthleticRecordStudentName.DataSource = studentDTO;
            ddlPrevAthleticRecordStudentName.DataTextField = "FullName";
            ddlPrevAthleticRecordStudentName.DataValueField = "StudentId";
            ddlPrevAthleticRecordStudentName.SelectedValue = SelectedStudent.ToString();
            ddlPrevAthleticRecordStudentName.DataBind();

        }
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }

        protected void btnTeamMembersMemberAdd_Click(object sender, EventArgs e)
        {
            AthleticRecordDTO athleticRecord = new AthleticRecordDTO
            {
                RecordId =Guid.NewGuid(),
                AthleticEventId = new Guid(ddlAthleticRecordAthleticEvent.SelectedValue),
                StudentId = new Guid(ddlAthleticRecordStudentName.SelectedValue),
                Year = ddlAthleticRecordYear.SelectedValue,
                TimeOrDistance =Convert.ToDecimal( txtAthleticRecordTimeorDist.Text),
                PreviousTimeOrDistance=Convert.ToDecimal(txtPrevAthleticRecordTimeorDist.Text),
                PreviousStudentId=new Guid(ddlPrevAthleticRecordStudentName.SelectedValue),
                LevelName=ddlAthleticRecordLevel.SelectedValue,
                //ModifiedBy = "Tharsan",
                ModifiedTime = System.DateTime.Now
            };

            athleticAL.SaveAthleticRecord(athleticRecord);

        }

        protected void btnCancelAthleticRecord_Click(object sender, EventArgs e)
        {

        }

        protected void ddlAthleticRecordAthleticEvent_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedAthleticEvent = new Guid(ddlAthleticRecordAthleticEvent.SelectedValue);
        }

        protected void ddlAthleticRecordStudentName_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedStudent = new Guid(ddlAthleticRecordStudentName.SelectedValue);
        }

        protected void ddlAthleticRecordYear_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        protected void ddlPrevAthleticRecordStudentName_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedStudent = new Guid(ddlPrevAthleticRecordStudentName.SelectedValue);

        }

        protected void ddlAthleticRecordLevel_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}